

/**
 *矛盾纠纷类型下拉
 */

import { Select, SelectProps } from 'antd';
import { memo, useImperativeHandle, useState } from 'react';

import { IRoleRestFillRefHandler } from '../../types';
import { useGetBeefType } from '../../service/beef-type-service';

interface IBeefTypeSelectProps extends SelectProps<number> {
    handleChange: (value: number) => void;
    onRef?: React.MutableRefObject<IRoleRestFillRefHandler | undefined>;
}

const BeefTypeSelect = ({ handleChange, onRef }: IBeefTypeSelectProps) => {
    const { isLoading, isError, data: dataSource } = useGetBeefType({
        beefTypeName: "",
        page: 1,
        limit: -1
    });

    const [val, setVal] = useState<number>();
    const onChange = (value: number) => {
        setVal(value);
        handleChange(value);
    };

    /**向外暴露的重置方法 */
    useImperativeHandle(onRef, () => ({
        rest: () => {
            setVal(undefined);
        },
        fill: (val: number) => {
            setVal(val);
        }
    }));
    return (

        <Select placeholder={'请选择矛盾纠纷类型'} value={val} onChange={onChange} loading={isLoading} allowClear>
            {isError
                ? null
                : dataSource?.list.map((data) => {
                    return <Select.Option key={data.beefTypeId} value={data.beefTypeId}>
                        {data.beefTypeName}
                    </Select.Option>
                })}
        </Select >

    );
};


export default memo(BeefTypeSelect)